Plotly and rOpenSci: Make ggplots shareable and interactive.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
By Matt Sundquist Plotly's Co-Founder
Here at Plotly, we are on a mission to build a platform where data scientists can analyze data, create beautiful graphs and collaborate: like a GitHub for data, where you can share and find plots, data, and code. The benefits are:
- Plots (including ggplot2 plots) are interactive and drawn with D3 (try zooming, panning, or click-and-drag).
- Plots shareable via URL and can be embedded in posts and articles. Even live-streaming ones!
- You can collaboratively create plots and analyze data in one place, all in the cloud, instead of emailing around files, data, code, and screenshots.
- You can edit plots with R, a web app, MATLAB, Python, Julia, and Perl.
- You can keep data and graphs together, and easily add data to your plots later.
- You can share all your work in one place, creating an online profile and repo of your projects.
Supporting open science is central to Plotly's mission. We are part of the rOpenSci project, dedicated to giving scientists access to tools and data in a way that promotes collaboration and enables reproducible work. We’re thrilled to support data and figure sharing.
Plotly lets you make ggplot2 plots, and then with one additional line of code, turn your plot into an interactive, online data visualization that you can edit with others. Plotly's R API lets users interact with plotly functions from their desktop R environment to create online graphs. Edit your graphs with others online with R or Plotly's online application.
Getting Started
The following code can get you started with Plotly and the plotly package.
install.packages(“devtools”)
library(devtools)
install_github(“plotly”, “ropensci”)
library(plotly)
## Loading required package: RCurl
## Loading required package: bitops
## Loading required package: RJSONIO
## Loading required package: ggplot2
library(ggplot2)
Sign up on Plot.ly or like this:
signup(“new_username”, “[email protected]”)
That should have responded with your key. Plug in your account data or you can use our “RgraphingAPI” account # # and key:
py <- plotly("RgraphingAPI", "ektgzomjbx")
Interactive Plots
First we'll draw a basic graph from the CO2 data set.
a <- qplot(conc, uptake, data = CO2, colour = Type) + scale_colour_discrete(name = "")
py$ggplotly(a)
When you run the plot, it will call it in your browser. It will make a graph with a URL, like this:
Note: here is the iframe that is rendering the plot in this post:
Iframes are a way to serve web content from one webpage onto another. In this case, you can serve Plotly graphs onto your blog, website, or RPubs, using this line of HTML code (and swapping in your URL):
You can fork that graph edit it, and use the underlying data. Here’s the data from that plot.
And you can use the GUI to analyze your data, make new plots, and copy and paste data to add it to a current plot. You can also update the figure using R.
Lines, Scattters and More
Next up, we'll run an example from an excellent tutorial by Toby Hocking. Toby is part of the ggplotly team and building support for ggplot2 syntax.
# Generate data
data <- data.frame(x=rep(1:10, times=5), group = rep(1:5, each=10))
data$lt <- c("even", "odd")[(data$group%%2+1)] # linetype
data$group <- as.factor(data$group)
data$y <- rnorm(length(data$x), data$x, .5) + rep(rnorm(5, 0, 2), each=10)
d <- ggplot() + geom_line(data=data, aes(x=x, y=y, colour=group, group=group, linetype=group)) + ggtitle(“geom_line + scale_linetype automatic”)
py$ggplotly(d)
The plot should look like this:
You can also edit the plot from the GUI, and save and share the new version, which will update your data, plot, and the figure:
The goal behind plotly is to make it easy to plug in your existing code, knowledge, and workflow from ggplot2 and R. The project is new and a good way to learn the ropes for Plotly’s own R API. We’d love to hear your thoughts, feedback, and ideas. We're on GitHub. We welcome your thoughts, issues, and pull requests.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.